package com.yhcnet.blog.controller;

import com.yhcnet.blog.reponse.EssayBody;
import com.yhcnet.blog.reponse.EssayDeleteBody;
import com.yhcnet.blog.reponse.EssayEditBody;
import com.yhcnet.blog.reponse.Resp;
import com.yhcnet.blog.service.EssayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@CrossOrigin
@RestController
@RequestMapping("/essay")
public class EssayController {

    @Autowired
    EssayService essayService;

    /**
     * 发表博文
     * @param essayBody
     * @return
     */
    @RequestMapping("/create")
    public Resp create(@RequestBody EssayBody essayBody){
        if(essayBody == null) return Resp.loginFail();
        if(essayService.create(essayBody)){
            return Resp.OK();
        }
        return Resp.loginFail();
    }

    @RequestMapping("/delete")
    public Resp delete(@RequestBody EssayDeleteBody essayDeleteBody){
        if(essayDeleteBody == null) return Resp.loginFail();
        if(essayService.delete(essayDeleteBody.getEid(),essayDeleteBody.getUuid(),essayDeleteBody.getEmail())){
           return Resp.OK();
        }
        return Resp.loginFail();
    }

    @RequestMapping("/edit")
    public Resp edit(@RequestBody EssayEditBody essayEditBody){
        if(essayEditBody == null) return Resp.loginFail();
        if(essayService.edit(essayEditBody)){
            return Resp.OK();
        }
        return Resp.loginFail();
    }

    @RequestMapping("/hot")
    public Resp getHot(){
        return Resp.OK(essayService.getHotEssay());
    }

    @RequestMapping("/new")
    public Resp getNew(){
        return Resp.OK(essayService.getListByLimit(1));
    }

    @RequestMapping("/EID")
    public Resp getByEID(int EID){
        return Resp.OK(essayService.getByEID(EID));
    }

    @RequestMapping("/count")
    public Resp count(){
        int count = essayService.count();
        return Resp.OK(count);
    }

    @RequestMapping("/kind")
    public Resp king(String kind){
        return Resp.OK(essayService.kind(kind));
    }

    @RequestMapping("/page")
    public Resp page(int page){
        return Resp.OK(essayService.getListByLimit(page));
    }

    @RequestMapping("/pagelist")
    public Resp pagelist(int page){
        return Resp.OK(essayService.getListBylist(page));
    }

    @RequestMapping("/visited")
    public Resp visited(){
        return Resp.OK(essayService.visitedall());
    }

    @RequestMapping("/email")
    public Resp getListByEmail(String email){
        return Resp.OK(essayService.getListByEmail(email));
    }
}
